<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use session;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Crypt;
class userController extends Controller
{
    public function add()
    {
    	return view('home.user.register');
    }
    //注册账号ajax测试
    public function ajaxAdd(Request $request)
    {
      $name = $request -> input('inputName');
      //dd($name);
      $res = DB::table('users') -> where('name',$name);
      if($res)
      {
      	return response() -> json(1);// 用户名存在  给个红
      }else{
      	return response() -> json(0);// 用户名不存在 给给绿
      }	

    }
    //注册******************************************
    public function insert(Request $request)
    {
    	$all = $request -> all();
      if($all['password'] != $all['rePassword'])
      {
        return back() -> with('密码不一致');
      }

      if($all['name']=='')
      {
        return back() -> with('用户名不存在');
      }
        $res = DB::table('users') -> where('name','=',$all['name']) -> get();
       
        if($res)
        {
         
          return back() -> with('用户名已存在');
        }
        $res = DB::table('users') -> where('email',$all['email']) -> get();
        if($res)
        {
          return back() -> with('此邮箱以被注册过');
        } 
        $res = DB::table('users') -> where('phone',$all['phone']) -> get();
        if($res)
        {
          return back() -> with('此手机号以被注册过');
        }
        //dd($all);
           $this -> validate($request,[
      	   'name'=>'required',
      	   'password'=>'required',
      	  
      	   'email'=>'required|email',
      	   'phone'=>'max:11|min:11'
      	                         ],[
            'name.required'=>'用户名不能为空',
            'password.required'=>'密码不能为空',
            'repassword.required'=>'确认密码不能为空',
            
            'email.required'=>'邮箱不能为空',
            'email.email'=>'邮箱格式不对',
            'phone.max'=>'手机号必须是11位的',
            'phone.min'=>'手机号必须是11位的',
      	                         ]
      	);
           $data = $request -> except('_token','rePassword');
           //密码加密
           $data['password'] = Crypt::encrypt($data['password']);
         
            date_default_timezone_set('PRC');
            $data['remember_token'] = str_random(70);
            $time = date("Y-m-d H:i:s");
            $data['created_at'] = $time;
            $data['userName'] = rand(10000000,99999999);
           // dd($data);
          $res = DB::table('users') -> insert($data);

           if($res)
           {
           	
           	return redirect('/home/user/login');

           }else{
           	return back();
           }

    }
    //前台 用户登录
    public function login()
    {    
    	return view('home.user.login');
    }
    //前台 用户登录执行
    public function doLogin(Request $request)
    {
      // echo $res = Crypt::encrypt('111');
      // die();
       $all = $request ->all();
       
        $res = DB::table('users') 
            -> where('name',$all['name'])
            -> first();
        if($res)
        {
           $password = Crypt::decrypt($res -> password);
           if($all['password'] == $password)
           {
           	 session(['master' => $res]);//保留session
           	 return redirect('/home/index');
           }else{
           	 return back() -> with('密码错误');
           }
        }else{
        	return back() -> with('用户名不存在');
        }	
    }
     //清空session*********************************************8
    public function logout()
    {
      session() -> flush();//清空session;
      return redirect('/home/user/login');
    }
    
     //个人信息**************************
    public function menu()
    {
    	return view('home.user.menu');

    }  
    //修改信息表单************************
    public function alter( )
    {
      return  view('home.user.alter');
    } 
    //修改信息执行***************************
    public function update(Request $request)
    {
      $data = $request ->all();
      $id = $data['id'];
     // dd($data);
     $res = DB::table('users') -> where('id',$data['id']) -> first();
     $password = Crypt::decrypt($res -> password);
     //dd($password);
      if($data['oldPassword']!=$password)
      {
        return back() -> with('原密码错误');
      }else{
          $this -> validate($request,[
           'userName'=>'required',
           'password'=>'required',
          
           'email'=>'required|email',
           'phone'=>'max:11|min:11'
                                 ],[
            'usrName.required'=>'用户名不能为空',
            'password.required'=>'密码不能为空',
            
            
            'email.required'=>'邮箱不能为空',
            'email.email'=>'邮箱格式不对',
            'phone.max'=>'手机号必须是11位的',
            'phone.min'=>'手机号必须是11位的'
                                 ]
           
        );
           $data = $request -> except('_token','oldPassword','id');
           //dd($data);
          //密码加密
           $data['password'] = Crypt::encrypt($data['password']);
           $res = DB::table('users')
             -> where('id',$id)
             -> update($data);
           if($res)
           {
            return redirect('/home/user/menu');
           }else{
            return back() -> with('');
           }
      } 
    }
    
}
